package cn.com.generaldata.os.service.common;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cn.com.generaldata.os.constant.FilterConstant;
import cn.com.generaldata.os.dao.intf.BaseDAOInterface;
import cn.com.generaldata.os.paging.Filter;
import cn.com.generaldata.os.paging.FilterRule;
import cn.com.generaldata.os.pojo.ReportHistory;

@Component
public class HistoryService {

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<ReportHistory> reportHistoryDAO;

	public List<ReportHistory> showHistory(String reportId) {
		Filter filter = new Filter();
		filter.setOrder(FilterConstant.DESC);
		filter.setSortBy("historyDate");

		filter.addRule(new FilterRule(FilterConstant.EQUAL, "report.id", reportId));
		return reportHistoryDAO.find(ReportHistory.class, filter, null);
	}

	public void buildHistory(ReportHistory reportHistory) {
		if (reportHistory == null) {
			// throw new
			// CommonException(ExceptionConstant.MSG_ILLEGAL_PARAMETER);
			return;
		}
		if (reportHistory.getId() == null || "".equals(reportHistory.getId()))
			reportHistory.setId(UUID.randomUUID().toString().replaceAll("-", ""));
		reportHistoryDAO.save(reportHistory);
	}

	public void setReportHistoryDAO(BaseDAOInterface<ReportHistory> reportHistoryDAO) {
		this.reportHistoryDAO = reportHistoryDAO;
	}

}
